<div class="layui-row">
    <form class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label w-auto">短剧id：</label>
            <div class="layui-input-inline">
                <input type="text" name="video_id" placeholder="请输入短剧id" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">短剧名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" placeholder="请输入短剧名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">album_id：</label>
            <div class="layui-input-inline">
                <input type="text" name="album_id" placeholder="请输入album_id" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label w-auto">审核状态：</label>
            <div class="layui-input-inline">
                <select name="audit_status" id="audit_status">
                    <option value="">请选择</option>
                    <option value="0">待送审</option>
                    <option value="1">不可播放</option>
                    <option value="2">可播放</option>
                    <option value="3">审核中</option>
                </select>
            </div>
        </div>
        <button class="layui-btn" style="margin-left: 10px;" lay-submit lay-filter="searchthirdVideo">搜索</button>
    </form>
</div>

<table id="dy-video" lay-filter="dy-video"></table>
<script type="text/html" id="dy-toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</script>

<script type="text/html" id="dy-tbBar">
    <a class="layui-btn  layui-btn-normal layui-btn-xs" lay-event="sync">同步</a>
    {{#  if(d.audit_status != 'auditing' ){ }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="audit">送审</a>
    {{#  } }}
    <a class="layui-btn layui-btn-xs" lay-event="online">上线</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="authorize">授权</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="look">查看</a>
</script>

<script>
    let audit_status_txt = {
        'none': '待送审',
        'auditing': '审核中',
        'fail': '不可播放',
        'success': '可播放'
    };

    let dy_labs = {
        1: '医神', 2: '赘婿', 3: '鉴宝', 4: '战神', 5: '娱乐明星', 6: '神医', 7: '重生', 8: '职场', 9: '逆袭', 10: '复仇',
        11: '青春', 12: '官场', 13: '家庭情感', 14: '乡村', 15: '正能量', 16: '成长', 17: '伦理', 18: '都市情感', 19: '社会话题', 20: '灵异',
        21: '悬疑推理', 22: '虐恋', 23: '甜宠', 24: '高干军婚', 25: '年代', 26: '萌宝', 27: '腹黑', 28: '总裁', 29: '宫斗宅斗', 30: '穿越',
        31: '种田经商', 33: '民俗', 34: '古装', 35: '穿越战争', 36: '现代军事', 37: '奇幻', 38: '科幻', 39: '架空玄幻', 40: '热血',
        41: '历史', 42: '搞笑', 43: '仙侠', 44: '武侠', 45: '二次元', 46: '其他'
    }

    function getLabTxt(ids) {
        let txt = '';
        if (!ids || ids.length <= 0) return txt;
        for (let i of ids) {
            if (dy_labs[i]) {
                txt += txt ? '、' + dy_labs[i] : dy_labs[i];
            }
        }
        return txt
    }

    let params = {
        elem: '#dy-video'
        , toolbar: '#dy-toolbarDemo'
        , url: '/admin/thirdVideo/list' //数据接口
        , page: true //开启分页
        , id: 'dy-video'
        , limit: 15
        , cols: [[//表头
            { field: 'id', title: 'ID', width: 80, sort: true, align: 'center' },
            { field: 'video_id', title: '短剧ID', width: 90, align: 'center' },
            {
                field: 'cover_img', title: '封面图片', width: 120, align: 'center',
                templet: function (d) {
                    if (d.cover_img) {
                        return `<div class="layer-photos"  style="cursor:pointer;height: 100%;">
                                    <img onclick="showimg(this)" style="height: 100%;" src=`+ d.cover_img + ` alt="">
                                </div>`;
                    } else {
                        return `<div>暂无照片</div>`
                    }
                }
            },
            { field: 'album_id', title: 'album_id', width: 180, align: 'center' },
            { field: 'name', title: '短剧名称',  align: 'center' },
            { field: 'sylloge', title: '集数', width: 80, align: 'center' },
            {
                title: '标签名', width: 150, align: 'center',
                templet: function (d) {
                    return d.config ? getLabTxt(d.config.tag_list) : ''
                }
            },
            {
                title: '审核状态', width: 90, align: 'center',
                templet: function (d) {
                    // return audit_status_txt[d.audit_status] ?? ''
                    //console.log(d.audit_status)
                     let str;
                   
                    switch (d.audit_status) {
                        case 'none':
                        str='#000'
                            break;

                        case 'auditing':
                            // case 2的代码块
                            str='#1e9fff'
                            break;

                        case 'fail':
                            // case N的代码块
                            str='red'
                            break;
                        case 'success':
                            // case N的代码块
                            str='#009688'
                            break;
                        default:
                        // default的代码块
                    }
                    return `<button type="button" style='background-color:${str};' class="layui-btn  layui-btn-xs ">${audit_status_txt[d.audit_status] ?? ''}</button>`
                }
            },
            { field: 'version', title: '审核版本', width: 80, align: 'center' },
            {
                field: 'online_status', title: '上线状态', width: 150, align: 'center',
                templet: function (d) {
                    return d.online_version != 0 ? '已上线(版本号：' + d.online_version + ')' : '未上线';
                }
            },
            {
                field: 'is_authorize', title: '授权状态', width: 90, align: 'center',
                templet: function (d) {
                    return d.is_authorize == 1 ? '已授权' : '未授权';
                }
            },

            { field: 'audit_at', title: '审核时间', width: 160, align: 'center' },
            { align: 'center', toolbar: '#dy-tbBar', title: '操作',width: 300 }
        ]]
    }

    tableList(params)

    layui.use(['table', 'form'], function () {
        let table = layui.table;
        let form = layui.form;
        table.on('toolbar(dy-video)', function (obj) {
            let event = obj.event;
            if (event === 'add') {
                layui.use(["form", "layer"], function () {
                    $ = layui.jquery;
                    let form = layui.form, layer = layui.layer;
                    layer.open({
                        type: 1,
                        title: '选择短剧',
                        btn: ['确定', '取消'],
                        area: ['600px', '300px'],
                        content: `
                                <div class="x-body chooseVideo" >
                                    <form class="layui-form" lay-filter="editTemplate">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">
                                                <span class="x-red">*</span>短剧选择
                                            </label>
                                            <div id="pVideo"></div>
                                        </div>
                                        <button id="submitTemplate" class="layui-btn" lay-submit lay-filter="submitTemplate" style="display: none;" />

                                    </form>
                                </div>
                            `,
                        success: function () {
                            let selectParams = {
                                url: '/admin/thirdVideo/videoSelect',
                                selected: [],
                                tips: '选择短剧',
                                name: 'video_id',
                                field_id: 'id',
                                field_Name: 'name',
                                elem: '#pVideo'
                            }
                            selectN(selectParams)
                        },
                        yes: function (mask) {
                            let data = form.val("editTemplate")
                            Ajax("{:url('admin/thirdVideo/add')}", { 'type': 2, 'video_id': data.video_id }).then(data => {
                                if (data.code === 200) {
                                    layer.msg(data.message, {
                                        icon: 6,
                                        time: 2000
                                    }, function () {
                                        if (mask) {
                                            layer.close(mask)
                                        }
                                        table.reloadData('dy-video')
                                    });
                                } else {
                                    layer.alert(data.message);
                                }
                            });
                        }
                    });
                });
            }
        });

        table.on('tool(dy-video)', function (obj) {
            var event = obj.event;
            switch (event) {
                case 'sync':
                    x_admin_show(`同步`, `./synchronization.html?id=${obj.data.id}`, 650, 550);
                    break;
                case 'audit':
                    AnyAjax('确认送审么？', '{:url("admin/mediaAssets/audit")}', { id: obj.data.id }, 'dy-video')
                    break;
                case 'online':
                    //AnyAjax('确认上线么？', '{:url("admin/mediaAssets/online")}', { id: obj.data.id }, 'dy-video')
                    layui.use(["form", "layer"], function () {
                        $ = layui.jquery;
                        let form = layui.form, layer = layui.layer;
                        layer.open({
                            type: 1,
                            title: '版本上线',
                            btn: ['确定', '取消'],
                            area: ['500px', '300px'],
                            content:
                                `<div class="x-body chooseVersion" >
                                    <form class="layui-form" lay-filter="editVersion">
                                        <div class="layui-form-item">
                                            <label class="layui-form-label">上线版本</label>
                                            <div class="layui-input-block">
                                              <input type="text" name="online_version" required  lay-verify="required" placeholder="请输入上线版本" autocomplete="off" class="layui-input" value="${obj.data.version ?? 0}">
                                            </div>
                                        </div>
                                        <button id="submitVersion" class="layui-btn" lay-submit lay-filter="submitVersion" style="display: none;" />
                                    </form>
                                </div>`,
                            yes: function (mask) {
                                let formParam = form.val("editVersion")
                                Ajax("{:url('admin/mediaAssets/online')}", { id: obj.data.id, 'online_version': formParam.online_version }).then(data => {
                                    if (data.code === 200) {
                                        layer.msg(data.message, {
                                            icon: 6,
                                            time: 2000
                                        }, function () {
                                            if (mask) {
                                                layer.close(mask)
                                            }
                                            table.reloadData('dy-video')
                                        });
                                    } else {
                                        layer.msg(data.message);
                                    }
                                });
                            }
                        });
                    });
                    break;
                case 'authorize':
                    //AnyAjax('确认授权么？', '{:url("admin/mediaAssets/authorize")}', { id: obj.data.id }, 'dy-video')
                    console.log(obj.data.applet_ids)
                    x_admin_show(`授权`, `./authorize.html?id=${obj.data.id}&data=${obj.data.applet_ids}`, 650, 550);
                    break;
                case 'look':
                    x_admin_show(`查看目录（短剧id:${obj.data.video_id} 短剧名称:${obj.data.name}）`, `./episodes.html?id=${obj.data.id}&video_name=${obj.data.name}`, 1150, 650, null);
                    break;
            }

        });

        form.on('submit(searchthirdVideo)', function (data) {
            params.where = data.field
            tableList(params)
            return false
        })
    });
</script>